rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004} ]

from operator import itemgetter
rows_by_fname = sorted(rows,key=itemgetter('fname'))
rows_by_uid = sorted(rows,key=itemgetter('uid'))
# itemgetter()函数也支持多个keys
rows_by_lfname = sorted(rows,key=itemgetter('fname','lname'))
# itemgetter()函数也可以被lambda表达式代替
rows_by_fname1 = sorted(rows,key=lambda x:x['fname'])
rows_by_uid1 = sorted(rows,key=lambda a: a['uid'])
rows_by_lfname1 = sorted(rows,key=lambda r:(r['fname'],r['uid']))
print(rows_by_fname1)
print(rows_by_fname)
print(rows_by_uid1)
print(rows_by_uid)
print(rows_by_lfname1)
print(rows_by_lfname)